﻿Imports System.Net
Imports System.Web.Http
Imports Models.Users

Namespace Controllers

    Public Class UserController
        Inherits ApiController

        ''' <summary>
        ''' Get User By Id
        ''' </summary>
        ''' <param name="id"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function [Get](id As Integer) As UserVO
            Try
                Dim oService As New Services.Users(Request)
                Dim oEntity As UserVO = oService.Get(id)
                Return oEntity
            Catch ex As Exception
                apiHelpers.ManageException(ex)
            End Try
        End Function


        ''' <summary>
        ''' Get List of Users
        ''' </summary>
        ''' <param name="companyId">Optional for filter</param>
        ''' <returns>Array of User</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("GET")> _
        Public Function List(Optional companyId As Integer = -1) As Generic.List(Of Models.Users.UserVO)
            ''Public Function List(Optional companyId As Integer = -1) As IEnumerable(Of UserCompanyVO)
            Try
                Dim oService As New Services.Users(Request)
                ''Dim oEntity As IEnumerable(Of UserCompanyVO) = oService.List(companyId)
                Return oService.List(companyId)
            Catch ex As Exception
                apiHelpers.ManageException(ex)
                Return Nothing
            End Try
        End Function

        ''' <summary>
        ''' Update User
        ''' </summary>
        ''' <param name="user">User object</param>
        ''' <returns>Boolean</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("PUT")> _
        Public Function Update(user As UserVO) As Integer
            Try
                Dim oService As New Services.Users(Request)
                Return oService.Update(user)
            Catch ex As Exception
                apiHelpers.ManageException(ex)
                Return -1
            End Try
        End Function

        ''' <summary>
        ''' Create a user
        ''' </summary>
        ''' <param name="user"></param>
        ''' <returns>The new ID  of the entity</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("POST")> _
        Public Function Create(user As UserVO) As Integer
            Try
                Dim oService As New Services.Users(Request)
                Return oService.Create(user)
            Catch ex As Exception
                apiHelpers.ManageException(ex)
                Return -1
            End Try
        End Function

        ''' <summary>
        ''' Delete a user
        ''' </summary>
        ''' <param name="id">UserId</param>
        ''' <returns>1 if OK</returns>
        ''' <remarks></remarks>
        <AcceptVerbs("DELETE")>
        Public Function Delete(id As Integer) As Integer
            Try
                Dim oService As New Services.Users(Request)
                Return oService.Delete(id)
            Catch ex As Exception
                apiHelpers.ManageException(ex)
                Return -1
            End Try
        End Function


        ' ''' <summary>
        ' ''' Quick Update User Preferece
        ' ''' </summary>
        ' ''' <param name="userPreference">FreeUserPreferenceVO object</param>
        ' ''' <returns>Integer</returns>
        ' ''' <remarks></remarks>
        '<AcceptVerbs("PUT")> _
        'Public Function UpdateUserPreference(userPreference As FreeUserPreferenceVO) As Integer
        '    Try
        '        Dim oService As New Services.Users(Request)
        '        Return oService.UpdateUserPreference(userPreference)
        '    Catch ex As Exception
        '        apiHelpers.ManageException(ex)
        '        Return -1
        '    End Try
        'End Function

        ' ''' <summary>
        ' ''' Quick Get User Preferece
        ' ''' </summary>
        ' ''' <param name="userPreference">FreeUserPreferenceVO object</param>
        ' ''' <returns>String</returns>
        ' ''' <remarks></remarks>
        '<AcceptVerbs("GET")> _
        'Public Function GetUserPreference(preferenceCode As String, userCompanyId As Integer) As String
        '    Try
        '        Dim oService As New Services.Users(Request)
        '        Return oService.GetUserPreference(preferenceCode, userCompanyId)
        '    Catch ex As Exception
        '        apiHelpers.ManageException(ex)
        '        Return -1
        '    End Try
        'End Function

    End Class

End Namespace